package com.zwb.boot.common.utils;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.NullArgumentException;
import org.springframework.util.ObjectUtils;


/**
 * @author 头发又黑又长
 * @Date 2022/7/24 9:47
 * @email zwb15083976291@163.com
 */

@Slf4j
public class SMSUtils {


    private final String accessKeyId;
    private final String accessKeySecret;
    //签名
    private String signName;
    //模板code
    private String templateCode;
    // 客户端
    private final com.aliyun.dysmsapi20170525.Client client;


    public SMSUtils(String accessKeyId, String accessKeySecret) throws Exception {
        this.accessKeyId = accessKeyId;
        this.accessKeySecret = accessKeySecret;
        client = this.createClient(this.accessKeyId, this.accessKeySecret);
    }

    public SMSUtils(String accessKeyId, String accessKeySecret, String signName, String templateCode) throws Exception {
        this.accessKeyId = accessKeyId;
        this.accessKeySecret = accessKeySecret;
        this.signName = signName;
        this.templateCode = templateCode;
        client = this.createClient(this.accessKeyId, this.accessKeySecret);
    }

    private com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    /**
     * @param code         验证码
     * @param PhoneNumbers 手机号码
     * @throws Exception
     */
    public String sendMessage(String code, String PhoneNumbers) throws Exception {
        if (ObjectUtils.isEmpty(this.signName) || ObjectUtils.isEmpty(this.templateCode)) {
            throw new NullPointerException("signName | templateCode 不能为空");
        }
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(PhoneNumbers)
                .setSignName(this.signName)
                .setTemplateCode(this.templateCode)
                .setTemplateParam("{\"code\":\"" + code + "\"}");
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = this.client.sendSmsWithOptions(sendSmsRequest, runtime);
            log.info(sendSmsResponse.getBody().toString());
            return sendSmsResponse.getBody().code;
        } catch (TeaException error) {
            log.error(error.message);
            // 如有需要，请打印 error
            return error.code;
        } catch (Exception _error) {
            log.error(_error.getMessage());
            throw _error;
        }

    }


    public String sendMessage(String signName, String templateCode, String code, String PhoneNumbers) throws Exception {
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(PhoneNumbers)
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setTemplateParam("{\"code\":\"" + code + "\"}");
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = this.client.sendSmsWithOptions(sendSmsRequest, runtime);
            log.info(sendSmsResponse.getBody().getMessage());
            return sendSmsResponse.getBody().code;
        } catch (TeaException error) {
            log.error(error.message);
            // 如有需要，请打印 error
            log.error(error.message);
            return error.code;
        } catch (Exception _error) {
            log.error(_error.getMessage());
            throw _error;
        }

    }

    public static void main(String[] args) throws Exception {
//        sendMessage("白天的博客", "SMS_184825163", "123456", "15011111111");
    }
}